package com.jhhc.StormSurgeForecast.StormSurge.ServiceImpl;


import com.jhhc.StormSurgeForecast.AstronomicalTide.Dao.BDMS_FBC_ASTROTD_AMapper;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Pojo.BDMS_ST_ASTROTD_F;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Service.BDMS_ST_ASTROTD_FService;
import com.jhhc.StormSurgeForecast.AstronomicalTide.Util.ReadUtil;
import com.jhhc.StormSurgeForecast.StormSurge.Dao.BDMS_FBC_ASTROTD_A1Mapper;
import com.jhhc.StormSurgeForecast.StormSurge.Dao.BDMS_ST_ASTROTD_FMapper;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.FBC.BDMS_FBC_ASTROTD_A;
import com.jhhc.StormSurgeForecast.StormSurge.Service.BDMS_FBC_ASTROTD_AService;
import com.jhhc.utils.RedisUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service("BDMS_FBC_ASTROTD_AService")
@RequiredArgsConstructor
public class BDMS_FBC_ASTROTD_AServiceImpl implements BDMS_FBC_ASTROTD_AService {

    private final BDMS_FBC_ASTROTD_AMapper bdms_fbc_astrotd_aMapper;

    private final BDMS_ST_ASTROTD_FService bdms_st_astrotd_fService;

    private final BDMS_FBC_ASTROTD_A1Mapper bdmsFbcAstrotdA1Mapper;

    private final RedisUtil redisTemplate;

    @Override
    public List<BDMS_FBC_ASTROTD_A> selectByGrid(Integer projectId, String startTime, String endTime, Integer grid) {
        return bdmsFbcAstrotdA1Mapper.selectByGrid(projectId, startTime, endTime, grid);
    }
    @Override
    public List<Float[]> GetAstronomical(Integer ProjectId, String StartTime,String EndTime) {

        List<BDMS_FBC_ASTROTD_A> bdms_fbc_astrotd_a = (List<BDMS_FBC_ASTROTD_A>)redisTemplate.get("Astronomical" + ProjectId + StartTime + EndTime);
        if (bdms_fbc_astrotd_a == null) {
            List<BDMS_FBC_ASTROTD_A> bdms_fbc_astrotd_as = bdms_fbc_astrotd_aMapper.GetAstronomical(ProjectId,StartTime,EndTime);
            redisTemplate.set("Astronomical" + ProjectId + StartTime + EndTime,bdms_fbc_astrotd_as);
            redisTemplate.expire("Astronomical" + ProjectId + StartTime + EndTime,600);
            bdms_fbc_astrotd_a = (List<BDMS_FBC_ASTROTD_A>)redisTemplate.get("Astronomical" + ProjectId + StartTime + EndTime);
        }
        List<Float[]> floatList = new ArrayList<>();
        for (int i = 0; i < bdms_fbc_astrotd_a.size(); i++) {
            List<Float> floats = new ArrayList<>();
            if (bdms_fbc_astrotd_a.get(i).getDtvalues() != null && !bdms_fbc_astrotd_a.get(i).getDtvalues().equals("")) {
                int start = 0;
                int end = 9;
                for (int j = 0; j < (bdms_fbc_astrotd_a.get(i).getDtvalues().length())/10; j++) {
                    String value = bdms_fbc_astrotd_a.get(i).getDtvalues().substring(start,end);
                    floats.add(Float.parseFloat(value));
                    start+=10;
                    end+=10;
                }
            }
            Float[] f = floats.toArray(new Float[0]);
            floatList.add(f);
        }
        return floatList;
    }

    @Override
    public int astronomicalTideInsert(String path, String pid){
        File file = new File(path);
        String pathData63;
//        String pathData61;
//        String pathData64;
        String pathDataTime;
        String time;
        int num = -1;
        int count;
        for (File m: Objects.requireNonNull(file.listFiles())){
            System.out.println(m.toString());
            pathDataTime = m + File.separator + "nhup1.txt";
            time = ReadUtil.readnhup1(pathDataTime);
            pathData63 = m + File.separator + "fort.63";
            List<BDMS_FBC_ASTROTD_A> aList63 = ReadUtil.read63(pathData63,pid,time);
            if (aList63 != null) {
                //先删后入
                bdms_fbc_astrotd_aMapper.deleteData(aList63.get(0).getTm(),aList63.get(aList63.size() - 1).getTm(),pid);
                System.out.println(aList63.get(0).getTm() + ":" + aList63.get(aList63.size() - 1).getTm());
                for (BDMS_FBC_ASTROTD_A bdmsFbcAstrotdA : aList63) {
                    count = bdms_fbc_astrotd_aMapper.batchInsert2(bdmsFbcAstrotdA);
                    num += count;
                }
            }
//            List<BDMS_FBC_ASTROTD_A> aList64 = ReadUtil.read64(pathData64,calSchemeId,time);
//            if (aList64 != null) {
//                for (BDMS_FBC_ASTROTD_A bdmsFbcAstrotdA : aList64) {
//                    count = bdms_fbc_astrotd_aMapper.batchInsert2(bdmsFbcAstrotdA);
//                    num += count;
//                }
//            }

        }
        return num;
    }

    @Override
    public int astronomicalTideInsertGrid(String path) {
        File file = new File(path);
        String pathDataTime;
        String time;
        int num = -1;
        for (File m: Objects.requireNonNull(file.listFiles())){
            System.out.println(m.toString());
            pathDataTime = m + File.separator + "nhup1.txt";
            time = ReadUtil.readnhup1(pathDataTime);
            List<BDMS_ST_ASTROTD_F> aList61 = ReadUtil.read61(m.toString(),time);
            num += bdms_st_astrotd_fService.insertBatch(aList61);
        }
        return num;
    }

    @Override
    public void astronomicalTideInsertA(String path, String calSchemeId,String time) {
        path = path + File.separator + "fort.63";
        File file = new File(path);
        List<BDMS_FBC_ASTROTD_A> aList63 = ReadUtil.read63(path,calSchemeId,time);
        if (aList63 != null) {
            for (BDMS_FBC_ASTROTD_A bdmsFbcAstrotdA : aList63) {
                bdms_fbc_astrotd_aMapper.batchInsert2(bdmsFbcAstrotdA);
            }
            System.out.println("入库完成：" + aList63.get(0).getTm() + "---" +
                    aList63.get(aList63.size() - 1).getTm());
        }
    }

}
